package com.hyt.it.ogt.pj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.pj.mapper.PieceMapper;
import com.hyt.it.ogt.pj.model.entity.Piece;
import com.hyt.it.ogt.pj.service.IPieceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 评卷块表 服务实现类
 * </p>
 *
 * @author qiaozj
 * @since 2020-07-09
 */
@Slf4j
@Service
public class PieceServiceImpl extends BaseServiceImpl<PieceMapper, Piece> implements IPieceService {

    @Autowired
    private PieceMapper pieceMapper;

    @Override
    public List<Piece> selectBySubject(String subjectId) {
        List<Piece> pieces = pieceMapper.selectBySubject(subjectId);
        return pieces;
    }
    
    @Override
	public List<Piece> selectAllPiecesByProjectIdAndSubject(String projectId, String subjectId) {
    	log.debug("## selectAllPiecesByProjectIdAndSubject params:{},{}", projectId, subjectId);
		List<Piece> allPieces = new ArrayList<Piece>();
		boolean flag = true;
		Integer pageSize = 200;
		Integer pageNum = 1;
		while (flag) {
			try {
				PageInfo<Piece> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
					pieceMapper.selectByProjectAndSubjectId(projectId, subjectId);
				});
				if (CollectionUtils.isNotEmpty(pageInfo.getList())) {
					allPieces.addAll(pageInfo.getList());
					pageNum++;
				} else {
					flag = false;
				}
			} catch (Exception e) {
				log.error("## selectByProjectAndSubjectId error", e);
				flag = false;
			}
		}
		return allPieces;
	}

    @Override
    public List<Piece> getMarkPiece(String projectId, String subjectId, String pieceCode) {
    	List<Piece> markPiece = pieceMapper.getTaskPiece(projectId, subjectId, pieceCode);
        return markPiece;
    }

    @Override
    public void updateFinishStatus(String projectId, String subjectId, String pieceCode, Boolean status) {
        pieceMapper.updateFinishStatus(projectId, subjectId, pieceCode, status);
    }

    @Override
    public Piece selectPaperPiece(String projectId, String subjectId, String paperId) {
        return pieceMapper.selectPaperPiece(projectId, subjectId, paperId);
    }

    @Override
    public Piece getTeacherPiece(String projectId, String subjectId, String userId){
        Piece piece = pieceMapper.getTeacherPiece(projectId, subjectId, userId);
        return piece;
    }

    @Override
    public List<Piece> selectByProjectIdSubjectId(String projectId, String subjectId) {
        return pieceMapper.selectByProjectIdSubjectId(projectId, subjectId);
    }
}
